#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "Data.h"

float 
Gauss(
	float mean, 
	float sigma)
{
  double 	x;
  double 	z;

  z = (double)rand() / RAND_MAX;
  if (z == 1.0) z = 0.9999999;
  x = sigma * sqrt(2.0 * log(1.0 / (1.0 - z) ));

  z = (double)rand() / RAND_MAX;
  if (z == 1.0) z = 0.9999999;
  return ( (float)(mean + x * cos(2 * PI *z) ));
}

void Channel(float es_n0, long dl_kan, int *wej, float *wyj)
{
  float 	mean = 0;
  float 	es = 1;
  float 	sygnal;
  float 	sigma;
  float 	s_n;
  long 		y;

  s_n = (float) pow (10, (es_n0 / 10));
  sigma = (float) sqrt (es / (2 * s_n) );

  for (y = 0 ; y < dl_kan ; y++)
    {
      sygnal = 2 * *(wej + y) - 1; 					/* zamiana z wart binarnej 0/1 na symbole -1/+1 */
      *(wyj + y) = sygnal + Gauss(mean, sigma);  	/* dodanie szumu */
    }
}